Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 183d4f2

Browse files
authored
Refactoring (#63)
* 📝 TODO: Add usage guidelines * Fix image position * 💩 Add less support * Refactoring * Choose minimal defaults * Add missing dependency Fixes #59 * CSS source maps * Add script in logs * Allow custom injections & script processors * Add coffee support
1 parent 5ab2f88 commit 183d4f2

File tree

11 files changed

+233
-100
lines changed

11 files changed

+233
-100
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "2.3.0-beta.2",
44
"description": "Roll .vue files",
55
"main": "dist/rollup-plugin-vue.common.js",
6-
"jsnext": "dist/rollup-plugin-vue.js",
6+
"module": "dist/rollup-plugin-vue.js",
77
"scripts": {
88
"test": "npm run lint && npm run build && npm run unit",
99
"build": "node config/build.js",
@@ -81,5 +81,9 @@
8181
"uglify-js": "^2.7.5",
8282
"vue-hot-reload-api": "^2.0.8",
8383
"yargs": "^6.6.0"
84+
},
85+
"optionalDependencies": {
86+
"coffee-script": "^1.12.4",
87+
"coffeescript-compiler": "^0.1.1"
8488
}
8589
}

src/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export default function vue (opts = {}) {
2727
}
2828
opts.compileTemplate = false;
2929
}
30-
} catch (e) {}
30+
} catch (e) {
31+
}
3132
/* eslint-enable */
3233

3334
const config = mergeOptions(DEFAULT_OPTIONS, opts)
@@ -44,14 +45,15 @@ export default function vue (opts = {}) {
4445
}
4546
},
4647
load (id) {
47-
if (id.indexOf('.vue.component.') > -1) {
48-
const parts = id.split('.')
49-
const component = parts.slice(0, parts.length - 4).join('.')
50-
const index = parseInt(parts[parts.length - 4])
48+
if (id.indexOf('.vue.component.') < 0) return null
5149

52-
return styles[component][index] || ''
53-
}
50+
const parts = id.split('.')
51+
const component = parts.slice(0, parts.length - 4).join('.')
52+
const index = parseInt(parts[parts.length - 4])
53+
54+
if (index < styles[component].length) return styles[component][index]
5455
},
56+
5557
async transform (source, id) {
5658
if (!filter(id) || !id.endsWith('.vue')) {
5759
debug(`Ignore: ${id}`)
@@ -69,7 +71,10 @@ export default function vue (opts = {}) {
6971

7072
ongenerate () {
7173
if (config.styleToImports !== true) {
72-
if (config.css === undefined || config.css === null) config.css = DEFAULT_OPTIONS.css
74+
if (config.css === undefined || config.css === null) {
75+
config.css = DEFAULT_OPTIONS.css
76+
}
77+
7378
compileStyle(styles, config)
7479
}
7580
}

src/injections.js

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/options.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import { templateJs, moduleJs, renderJs } from './injections'
2+
import { coffee } from './script/index'
3+
14
export default {
5+
// Style compilation choices.
6+
styleToImports: false,
7+
autoStyles: true,
8+
disableCssModuleStaticReplacement: false,
9+
10+
// Config for html-minifier.
211
htmlMinifier: {
312
customAttrSurround: [[/@/, new RegExp('')], [/:/, new RegExp('')]],
413
collapseWhitespace: true,
514
removeComments: true
615
},
16+
17+
// Handle with(this)
718
vue: {
819
// Remove all transforms added by vue since it's up to the user
920
// to use whatever he wants
@@ -34,12 +45,39 @@ export default {
3445
unicodeRegExp: false
3546
}
3647
},
37-
styleToImports: false,
38-
autoStyles: true,
39-
disableCssModuleStaticReplacement: false,
48+
49+
// Config for postcss-modules.
4050
cssModules: {
4151
generateScopedName: '[name]__[local]'
4252
},
53+
54+
// Config for node-sass.
4355
scss: {},
44-
pug: {}
56+
57+
// Config for pug compiler.
58+
pug: {},
59+
60+
// Custom injectors.
61+
inject: {
62+
template: {
63+
js: templateJs,
64+
babel: templateJs
65+
},
66+
67+
render: {
68+
js: renderJs,
69+
babel: renderJs
70+
},
71+
72+
module: {
73+
js: moduleJs,
74+
babel: moduleJs
75+
}
76+
},
77+
78+
// script languages.
79+
script: {
80+
coffee,
81+
coffeescript: coffee
82+
}
4583
}

src/script/coffee.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Compiler from 'coffeescript-compiler'
2+
3+
const coffee = new Compiler()
4+
5+
export default function (script) {
6+
return new Promise((resolve, reject) => {
7+
coffee.compile(script.code, { bare: true }, (status, output) => {
8+
if (status === 0) {
9+
script.code = output
10+
11+
resolve(script)
12+
} else {
13+
reject(`Coffee compiler exited with status code ${status}.`)
14+
}
15+
})
16+
})
17+
}

src/script/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as coffee } from './coffee'

0 commit comments

Comments
 (0)