Skip to content

Commit 75ca0aa

Browse files
author
Robbie Cook
committed
fix: fixing extensions
fix: add tests fix: fix fix: fix
1 parent ca33c31 commit 75ca0aa

File tree

6 files changed

+22
-9
lines changed

6 files changed

+22
-9
lines changed

src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { dirname, extname, resolve } = require('path')
1+
const { dirname, resolve } = require('path')
22
const transform = require('./transform')
33

44
const defaultOptions = {
@@ -17,10 +17,11 @@ const getVariableName = p => {
1717
}
1818

1919
const applyTransform = (p, t, state, value, calleeName) => {
20-
const ext = extname(value)
2120
const options = Object.assign({}, defaultOptions, state.opts)
2221

23-
if (options.extensions && options.extensions.indexOf(ext.slice(1)) >= 0) {
22+
const valueMatches = options.extensions.some(ext => value.endsWith(ext))
23+
24+
if (options.extensions && valueMatches) {
2425
try {
2526
const rootPath = state.file.opts.sourceRoot || process.cwd()
2627
const scriptDirectory = dirname(resolve(state.file.opts.filename))

src/transform.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ function transform (rootPath, filePath, opts) {
8282

8383
const parsed = path.parse(filePath)
8484

85-
if (parsed.ext) {
86-
ext = parsed.ext.substr(1)
87-
}
85+
ext = parsed.base.slice(parsed.base.indexOf('.') + 1)
8886

8987
let basePath
9088

test/__snapshots__/index.test.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`index doesnt inline file when the lenght equals the limit 1`] = `"const test = \\"/public/test/assets/file.txt\\";"`;
3+
exports[`index doesnt inline file when the length equals the limit 1`] = `"const test = \\"/public/test/assets/file.txt\\";"`;
44

55
exports[`index handles base26 digest 1`] = `"const test = \\"/public/ccjesbcwzsppzgsqtbnioeblcexw.png\\";"`;
66

@@ -34,6 +34,8 @@ exports[`index handles hash as query param 1`] = `"const test = \\"/public/test/
3434

3535
exports[`index handles hex digest 1`] = `"const test = \\"/public/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
3636

37+
exports[`index handles multiple extensions 1`] = `"const test = \\"/static/934823cbc67ccf0d67aa2a2eeb798f12.module.less\\";"`;
38+
3739
exports[`index handles name with custom hash length 1`] = `"const test = \\"/public/9c87cbf3.png\\";"`;
3840

3941
exports[`index handles sha1 hash 1`] = `"const test = \\"/public/f824ad7c5f655c81c88d3fe267fe5780055bced5.png\\";"`;

test/assets/file.module.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background-color: red;
3+
}

test/fixtures/import-multiple.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import test from '../assets/file.module.less'

test/index.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ describe('index', function () {
2525
expect(result).toMatchSnapshot()
2626
})
2727

28+
it('handles multiple extensions', function () {
29+
const result = transformCode(getFixtures('import-multiple.js'), {
30+
publicPath: '/static',
31+
extensions: ['module.less']
32+
}).code
33+
expect(result).toMatchSnapshot()
34+
})
35+
2836
it('handles custom import path with trailing slash', function () {
2937
const result = transformCode(getFixtures('import-image.js'), {
3038
publicPath: '/static/'
@@ -260,7 +268,7 @@ describe('index', function () {
260268
expect(fs.existsSync(path.resolve(__dirname, './public'))).toEqual(false)
261269
})
262270

263-
it('doesnt inline file when the lenght equals the limit', function () {
271+
it('doesnt inline file when the length equals the limit', function () {
264272
const result = transformCode(getFixtures('import-text.js'), {
265273
extensions: ['txt'],
266274
name: '[path][name].[ext]',
@@ -269,7 +277,7 @@ describe('index', function () {
269277
expect(result).toMatchSnapshot()
270278
})
271279

272-
it('ouputs the file when the lenght equals the limit', function () {
280+
it('ouputs the file when the length equals the limit', function () {
273281
transformCode(getFixtures('import-text.js'), {
274282
extensions: ['txt'],
275283
name: '[path][name].[ext]',

0 commit comments

Comments
 (0)