Skip to content

Commit

Permalink
minor patches
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuaz committed Aug 21, 2022
1 parent c59447b commit 04bfc23
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"node": ">= 12.22.1"
},
"scripts": {
"build": "tsc && cp ./src/cjs.js ./lib",
"build": "tsc",
"lint": "eslint",
"test:clean": "find test/**/build/ -name '*.jpg' -o -name '*.png' -o -name '*.avif' -o -name '*.webp' -o -name '*.jpeg' -o -name '*.js' | xargs rm -f",
"test": "npm run build && npm run test:clean && webpack --config=./test/jimp/webpack.config.js && webpack --config=./test/sharp/webpack.config.js && jest"
Expand Down
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export default function loader(this: LoaderContext<Options>, content: string): v
placeholderSize,
mime,
sizes,
esModule: options.esModule,
}
orchestrate({ cacheOptions, transformParams })
.then((result) => loaderCallback(null, result))
Expand All @@ -147,7 +146,7 @@ async function orchestrate(params: OrchestrateParams) {
}

// Transform based on the parameters
export async function transform({
async function transform({
adapterModule,
resourcePath,
createFile,
Expand All @@ -156,7 +155,6 @@ export async function transform({
outputPlaceholder,
placeholderSize,
adapterOptions,
esModule,
}: TransformParams): Promise<string> {
const adapter: Adapter = adapterModule || require('./adapters/sharp')
const img = adapter(resourcePath)
Expand All @@ -177,7 +175,7 @@ export async function transform({
// default to the biggest image
const defaultImage = files[files.length - 1]

return `${esModule ? 'export default' : 'module.exports ='} {
return `${adapterOptions.esModule ? 'export default' : 'module.exports ='} {
srcSet: ${srcset},
images: [${images}],
src: ${defaultImage.path},
Expand Down
1 change: 0 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ export interface TransformParams {
placeholderSize: number
mime: MimeType
sizes: number[]
esModule: boolean
adapterOptions: Options & ImageOptions
}
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const getOutputAndPublicPath: GetOutputAndPublicPath = (
if (typeof configOutputPath === 'function') {
outputPath = configOutputPath(fileName)
} else {
outputPath = path.posix.join(configOutputPath, fileName)
outputPath = path.join(configOutputPath, fileName)
}
}
let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`
Expand All @@ -155,7 +155,7 @@ const getOutputAndPublicPath: GetOutputAndPublicPath = (
if (typeof configPublicPath === 'function') {
publicPath = configPublicPath(fileName)
} else {
publicPath = path.posix.join(configPublicPath, fileName)
publicPath = path.join(configPublicPath, fileName)
}
publicPath = JSON.stringify(publicPath)
}
Expand Down
29 changes: 29 additions & 0 deletions test/jimp/build/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,35 @@ Object {
}
`;

exports[`public path should replace global publicPath absolute 1`] = `
Object {
"default": Object {
"height": 900,
"images": Array [
Object {
"height": 450,
"path": "/public/c869fe04ebafd01d-500.jpg",
"width": 500,
},
Object {
"height": 675,
"path": "/public/76a7b8dd076af418-750.jpg",
"width": 750,
},
Object {
"height": 900,
"path": "/public/076d6ca8665b1d94-1000.jpg",
"width": 1000,
},
],
"src": "/public/076d6ca8665b1d94-1000.jpg",
"srcSet": "/public/c869fe04ebafd01d-500.jpg 500w,/public/76a7b8dd076af418-750.jpg 750w,/public/076d6ca8665b1d94-1000.jpg 1000w",
"toString": [Function],
"width": 1000,
},
}
`;

exports[`single size 1`] = `
Object {
"default": Object {
Expand Down
4 changes: 4 additions & 0 deletions test/jimp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ test('public path should replace global publicPath', () => {
const multi = require('../cat-1000.jpg?outputPath=img/&publicPath=public/')
expect(multi).toMatchSnapshot()
})
test('public path should replace global publicPath absolute', () => {
const multi = require('../cat-1000.jpg?outputPath=/img2/&publicPath=/public/')
expect(multi).toMatchSnapshot()
})

test('with placeholder image', () => {
const output = require('../cat-1000.jpg?placeholder=true')
Expand Down
15 changes: 12 additions & 3 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { getOutputAndPublicPath } from '../lib/utils'

describe('Utils package', () => {
it('should create both paths', () => {
it('should create both paths respecting absolutes', () => {
const { outputPath, publicPath } = getOutputAndPublicPath('file.png', {
outputPath: '/dist/img/',
publicPath: '/img',
publicPath: '/public',
})
expect(outputPath).toBe('/dist/img/file.png')
expect(publicPath).toBe('"/img/file.png"')
expect(publicPath).toBe('"/public/file.png"')
})

it('should create both paths ', () => {
const { outputPath, publicPath } = getOutputAndPublicPath('file.png', {
outputPath: 'dist/img/',
publicPath: 'public/',
})
expect(outputPath).toBe('dist/img/file.png')
expect(publicPath).toBe('"public/file.png"')
})
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"lib": ["es2018"],
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"outDir": "lib",
"allowJs": true,
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
"moduleResolution": "node",
Expand All @@ -14,5 +15,5 @@
/* Additional Checks */
"resolveJsonModule": true
},
"include": ["src/**/*.ts"],
"include": ["src/**/*"],
}

0 comments on commit 04bfc23

Please sign in to comment.