Skip to content

Commit

Permalink
Added check for surplus dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Jun 3, 2019
1 parent 23b83c2 commit 26a1c28
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
2 changes: 2 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`#transform should not try to parse with the surplus compiler 1`] = `"received is not a function"`;

exports[`#transform should transform the jsx in input.js 1`] = `
"\\"use strict\\";
Expand Down
28 changes: 24 additions & 4 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,34 @@ describe('#initialization', () => {
describe('#transform', () => {
const Bundler = require('parcel-bundler')
const surplusPlugin = require('../index')
let bundler = new Bundler(inputPath, {
watch: false
})
surplusPlugin(bundler)

it('should transform the jsx in input.js', async () => {
expect.assertions(1)

let bundler = new Bundler(inputPath, {
watch: false
})

surplusPlugin(bundler)

const result = await bundler.bundle()

expect(result.entryAsset.generated.js.trim()).toMatchSnapshot()
})

it('should not try to parse with the surplus compiler', async () => {
expect.assertions(1)

let bundler = new Bundler(`${__dirname}/samples/no-surplus-view.js`, {
watch: false
})

surplusPlugin(bundler)

try {
await bundler.bundle()
} catch (e) {
expect(e).toThrowErrorMatchingSnapshot()
}
})
})
6 changes: 6 additions & 0 deletions __tests__/samples/no-surplus-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// It should not try to parse with the surplus compiler
const view = (
<div>
<h2>Surplus</h2>
</div>
)
12 changes: 12 additions & 0 deletions lib/js-asset.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const Asset = require('parcel-bundler/src/assets/JSAsset')
const SourceMap = require('parcel-bundler/src/SourceMap')
const compiler = require('surplus/compiler')
const DepsRegex = require('deps-regex')

const re = new DepsRegex({
matchInternal: true,
matchES6: true
})

const isUsingSurplus = contents => re.getDependencies(contents).find(d => d === 'surplus')

class JSAsset extends Asset {
async pretransform () {
if (!isUsingSurplus(this.contents)) {
return super.pretransform()
}

if (this.options.sourceMaps) {
const { src, map } = compiler.compile(this.contents, {
sourcemap: 'extract',
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@
"surplus": "^0.5.2"
},
"devDependencies": {
"@geut/chan": "^1.3.0-1",
"jest": "^23.4.0",
"parcel-bundler": "^1.9.7",
"@geut/chan": "^2.0.0",
"jest": "^24.8.0",
"parcel-bundler": "^1.12.3",
"s-js": "^0.4.7",
"standard": "^11.0.1",
"standard": "^12.0.1",
"surplus": "^0.5.2"
},
"dependencies": {
"deps-regex": "^0.1.4"
}
}

0 comments on commit 26a1c28

Please sign in to comment.